import Vue from "vue";
import VueRouter from "vue-router";
import navConf from "../nav.config.json";

Vue.use(VueRouter);

const commonRoutes = [
  {
    path: "/",
    name: "index",
    component: () => import("../views/Home.vue"),
  },
];

let markdownRoutes = [];

Object.keys(navConf).forEach((header) => {
  markdownRoutes = markdownRoutes.concat(navConf[header]);
});

let addComponent = (router) => {
  router.forEach((route) => {
    if (route.items) {
      addComponent(route.items);
      markdownRoutes = markdownRoutes.concat(route.items);
    } else {
      route.component = () => import(`../docs/${route.name}.md`);
    }
  });
};

addComponent(markdownRoutes);

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes: [...commonRoutes, ...markdownRoutes],
});

export default router;
